<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Profesores extends Main_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('profesorModel');
        $this->load->model('barrioModel');
        $this->load->model('carreraModel');
        $this->load->model('partidoModel');
        $this->load->model('comunaModel');
        $this->load->model('materiaModel');
        $this->load->helper('url');
    }

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     * 		http://example.com/index.php/welcome
     * 	- or -  
     * 		http://example.com/index.php/welcome/index
     * 	- or -
     * Since this controller is set as the default controller in 
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see http://codeigniter.com/user_guide/general/urls.html
     */
    public function index() {

        $data = array();
        if(PERFIL_ADMINISTRADOR == SessionData::getPerfil()){
            $data['profesores'] = $this->profesorModel->getAll();
        }else{
            $data['profesores'] = $this->profesorModel->getForList(SessionData::getId());
        }


        $this->load->view('include/header', $this->getHeaderData());
        $this->load->view('profesores_list', $data);
        $this->load->view('include/footer');
    }
    
    public function user($id) {        
        $data = array();
        $this->load->model('usuarioModel');
        $data['usuario'] = $this->usuarioModel->getByProfesor($id);
        
         if ($this->input->post() != null) {
             if($this->input->post('new_pass') == $this->input->post('new_pass_2')){
                 if(!isset($data['usuario']['contrasena']) || $data['usuario']['contrasena'] == sha1($this->input->post('pass'))){
                     if($this->usuarioModel->tieneUsuario($id)){
                        if($this->usuarioModel->noExiste($id,$this->input->post('usuario'))){
                            $this->usuarioModel->modificar($id,$this->input->post('usuario'), sha1($this->input->post('new_pass')));
                            redirect('profesores/');
                        }else{
                            $data['errorMsg'] = 'El usuario ya existe';
                        }
                     }else{
                         $this->usuarioModel->crear($id,$this->input->post('usuario'), sha1($this->input->post('new_pass')));
                         redirect('profesores/');
                     }
                 }else{
                     $data['errorMsg'] = 'Las contraseña vieja no coincide';
                 }
             }else{
                 $data['errorMsg'] = 'Las contraseñas no son iguales';
             }
         }

        $this->load->view('include/header', $this->getHeaderData());
        $this->load->view('profesor_usuario', $data);
        $this->load->view('include/footer');
    }

    public function update($id) {

        $data = array();

        if (!empty($_POST)) {
            try{
            $this->db->trans_begin();
            $this->profesorModel->update($id, $this->input->post('Profesor'), 
                    $this->input->post('carrera'), 
                    $this->input->post('telefono'), 
                    $this->input->post('mail'), 
                    $this->input->post('facebook'), 
                    $this->input->post('direccion'),
                    (int)$this->input->post('enDomicilio'),
                    (int)$this->input->post('enFacultad'),(int)$this->input->post('puntosBase'));
            $this->profesorModel->deleteBarriosAD($id);
            $this->profesorModel->deletePartidosAD($id);
            $this->profesorModel->deleteMaterias($id);
            //print_r($this->input->post('barrios'));exit;
            if($this->input->post('comunas') != ''){
                foreach ($this->input->post('comunas') as $comuna) {
                    $this->profesorModel->setComuna($id, $comuna);
                }
            }
            if ($this->input->post('barrios') != '') {
                foreach ($this->input->post('barrios') as $barrio) {
                    if(!$this->profesorModel->existBarrioAD($id, $barrio)){
                        $this->profesorModel->setBarrioAD($id, $barrio);
                    }
                }
            }
            if ($this->input->post('partidos') != '') {
                foreach ($this->input->post('partidos') as $partido) {
                    $this->profesorModel->setPartidoAD($id, $partido);
                }
            }
            if ($this->input->post('materias') != '') {
                foreach ($this->input->post('materias') as $materia) {
                    $this->profesorModel->setMateria($id, $materia);
                }
            }
            $this->db->trans_commit();
            $data['msg'] = 'Profesor modificado satisfactoriamente.';
            
            }catch(Exception $e){
                $this->db->trans_rollback();
                $data['error'] = 'Profesor no pudo ser modificado.';
            }
        }


        $data['profesor'] = $this->profesorModel->get($id);
        $data['materiasProfesor'] = $this->profesorModel->getMaterias($id);
        $data['barriosProfesor'] = $this->profesorModel->getBarriosAD($id);
        $data['partidosProfesor'] = $this->profesorModel->getPartidosAD($id);
        $data['comunasProfesor'] = $this->profesorModel->getComunasAD($id);
        $data['barrios'] = $this->barrioModel->getAll();
        $data['carreras'] = $this->carreraModel->getAll();
        $data['partidos'] = $this->partidoModel->getAll();
        $data['comunas'] = $this->comunaModel->getAll();
        $data['materias'] = $this->materiaModel->getAll()->result();
        $this->load->library('searchQueryResult');
        $data['searcher'] = $this->searchqueryresult->getInstance();        
        $this->load->view('include/header',$this->getHeaderData());
        $this->load->view('profesores_form', $data);
        $this->load->view('include/footer');
    }

    public function delete($id) {
        $data = array();
        $this->profesorModel->delete($id);
        $data['msg'] = 'El profesor fue eliminado';
        $data['errorCode'] = 0;
        echo json_encode($data);
    }

    public function add() {
        $this->load->helper('url');
        $data = array();
        if (!empty($_POST)) {

            $created = $this->profesorModel->create($this->input->post('Profesor'), $this->input->post('carrera'), $this->input->post('telefono'), 
                    $this->input->post('mail'), $this->input->post('facebook'), $this->input->post('direccion'),
                    (int)$this->input->post('enDomicilio'),
                    (int)$this->input->post('enFacultad'),(int)$this->input->post('puntosBase'));
            if($this->input->post('comunas') != ''){
                foreach ($this->input->post('comunas') as $comuna) {
                    $this->profesorModel->setComuna($created, $comuna);
                }
            }
            if($this->input->post('barrios') != ''){
                foreach ($this->input->post('barrios') as $barrio) {
                    if(!$this->profesorModel->existBarrioAD($created, $barrio)){
                        $this->profesorModel->setBarrioAD($created, $barrio);
                    }
                }
            }
            if($this->input->post('partidos') != ''){
                foreach ($this->input->post('partidos') as $partido) {
                    $this->profesorModel->setPartidoAD($created, $partido);
                }
            }
            if($this->input->post('materias') != ''){
                foreach ($this->input->post('materias') as $materia) {
                    $this->profesorModel->setMateria($created, $materia);
                }
            }
            
            if ($created > 0) {
                $data['msg'] = 'Profesor creado satisfactoriamente.';
            }
        }
        $data['barrios'] = $this->barrioModel->getAll();
        $data['carreras'] = $this->carreraModel->getAll();
        $data['partidos'] = $this->partidoModel->getAll();
        $data['comunas'] = $this->comunaModel->getAll();
        $data['materias'] = $this->materiaModel->getAll()->result();

        $this->load->view('include/header',$this->getHeaderData());
        $this->load->view('profesores_form', $data);
        $this->load->view('include/footer');
    }
    
    public function setBandaHoraria($profesorId){
        $data = array();
        $this->load->model('bandaHorariaModel');
       
        $data['bandasHorarias'] = $this->bandaHorariaModel->getByProfesor($profesorId);
        $this->load->model('diaModel');
        $data['dias'] = $this->diaModel->getAll();
        $data['profesorId'] = $profesorId;
        $this->load->view('include/header',$this->getHeaderData());
        $this->load->view('bandas_horarias', $data);
        $this->load->view('include/footer');
    }
    
    public function createBandaHoraria($profesorId){
         $this->load->model('bandaHorariaModel');
        $created = $this->bandaHorariaModel->crear($profesorId,$this->input->post('desde'),$this->input->post('hasta'),$this->input->post('dia'));
        if ($created > 0) {
            $data['msg'] = 'Banda horaria creada satisfactoriamente.';
        }
         $this->setBandaHoraria($this->input->post('profesorId'));
    }
    
    public function updateBandaHoraria($banda){  
        $this->load->model('bandaHorariaModel');
        if($this->input->post('send') == 'update'){
            
        }else{
            $this->bandaHorariaModel->delete($banda);
        }
        $this->setBandaHoraria($this->input->post('profesorId'));
    }

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */